package com.tianji.promotion.strategy.discount;

import com.tianji.common.utils.NumberUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.promotion.anoontation.DiscountTypeFlag;
import com.tianji.promotion.domain.po.Coupon;
import com.tianji.promotion.enums.DiscountType;
import org.springframework.stereotype.Component;

import java.math.RoundingMode;

@Component
@DiscountTypeFlag(value = DiscountType.PRICE_DISCOUNT)
public class PriceDiscount implements Discount{

    private static final String RULE_TEMPLATE = "满{}减{}";


    @Override
    public boolean canUse(int totalAmount, Coupon coupon) {
        return totalAmount >= coupon.getThresholdAmount();
    }

    @Override
    public int calculateDiscount(int totalAmount, Coupon coupon) {
        return coupon.getDiscountValue();
    }

    @Override
    public String getRule(Coupon coupon) {
        return StringUtils.format(
                RULE_TEMPLATE,
                NumberUtils.roundStr(coupon.getThresholdAmount(), 2, RoundingMode.UP),
                NumberUtils.roundStr(coupon.getDiscountValue(), 2, RoundingMode.UP)
        );
    }
}
